<HTML>
<HEAD>
<!-- Created by texi2html 1.56k from /home/jaffer/scheme/r5rs.txi on 28 March 2001 -->

<TITLE>Revised(5) Scheme - 3. Basic concepts</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="r5rs_1.html">first</A>, <A HREF="r5rs_4.html">previous</A>, <A HREF="r5rs_6.html">next</A>, <A HREF="r5rs_14.html">last</A> section, <A HREF="r5rs_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC18" HREF="r5rs_toc.html#TOC18">3. Basic concepts</A></H1>

<P>
<A NAME="IDX41"></A>




<H2><A NAME="SEC19" HREF="r5rs_toc.html#TOC19">3.1 Variables; syntactic keywords; and regions</A></H2>

<P>
<A NAME="IDX42"></A>


<P>
An identifier may name a type of syntax, or it may name
<A NAME="IDX43"></A>
a location where a value can be stored.  An identifier that names a type
of syntax is called a <EM>syntactic keyword</EM>
<A NAME="IDX44"></A>
and is said to be <EM>bound</EM> to that syntax.  An identifier that names a
location is called a <EM>variable</EM> and is said to be
<A NAME="IDX45"></A>
<EM>bound</EM> to that location.  The set of all visible
bindings in effect at some point in a program is
<A NAME="IDX46"></A>
known as the <EM>environment</EM> in effect at that point.  The value
stored in the location to which a variable is bound is called the
variable's value.  By abuse of terminology, the variable is sometimes
said to name the value or to be bound to the value.  This is not quite
accurate, but confusion rarely results from this practice.


<P>
Certain expression types are used to create new kinds of syntax
and bind syntactic keywords to those new syntaxes, while other
expression types create new locations and bind variables to those
locations.  These expression types are called <EM>binding constructs</EM>.


<P>
<A NAME="IDX47"></A>
Those that bind syntactic keywords are listed in section <A HREF="r5rs_6.html#SEC39">4.3 Macros</A>.
The most fundamental of the variable binding constructs is the
<SAMP>`lambda'</SAMP> expression, because all other variable binding constructs
can be explained in terms of <SAMP>`lambda'</SAMP> expressions.  The other
variable binding constructs are <SAMP>`let'</SAMP>, <SAMP>`let*'</SAMP>, <SAMP>`letrec'</SAMP>,
and <SAMP>`do'</SAMP> expressions (see sections see section <A HREF="r5rs_6.html#SEC29">4.1.4 Procedures</A>, see section <A HREF="r5rs_6.html#SEC34">4.2.2 Binding constructs</A>, and
see section <A HREF="r5rs_6.html#SEC36">4.2.4 Iteration</A>).


<P>
Like Algol and Pascal, and unlike most other dialects of Lisp
except for Common Lisp, Scheme is a statically scoped language with
block structure.  To each place where an identifier is bound in a program
there corresponds a <EM>region</EM> of the program text within which
<A NAME="IDX48"></A>
the binding is visible.  The region is determined by the particular
binding construct that establishes the binding; if the binding is
established by a <SAMP>`lambda'</SAMP> expression, for example, then its region
is the entire <SAMP>`lambda'</SAMP> expression.  Every mention of an identifier
refers to the binding of the identifier that established the
innermost of the regions containing the use.  If there is no binding of
the identifier whose region contains the use, then the use refers to the
binding for the variable in the top level environment, if any
(chapters see section <A HREF="r5rs_6.html#SEC24">4. Expressions</A> and see section <A HREF="r5rs_8.html#SEC48">6. Standard procedures</A>); if there is no
binding for the identifier,
it is said to be <EM>unbound</EM>.
<A NAME="IDX49"></A>
<A NAME="IDX50"></A>
<A NAME="IDX51"></A>




<H2><A NAME="SEC20" HREF="r5rs_toc.html#TOC20">3.2 Disjointness of types</A></H2>

<P>
<A NAME="IDX52"></A>


<P>
No object satisfies more than one of the following predicates:



<PRE>

boolean?          pair?
symbol?           number?
char?             string?
vector?           port?
procedure?

</PRE>

<P>
These predicates define the types <EM>boolean</EM>, <EM>pair</EM>, <EM>symbol</EM>, <EM>number</EM>, <EM>char</EM> (or <EM>character</EM>), <EM>string</EM>, <EM>vector</EM>, <EM>port</EM>, and <EM>procedure</EM>.  The empty list is a special
object of its own type; it satisfies none of the above predicates.


<P>
<A NAME="IDX53"></A>
<A NAME="IDX54"></A>
<A NAME="IDX55"></A>
<A NAME="IDX56"></A>


<P>
<A NAME="IDX57"></A>
<A NAME="IDX58"></A>
<A NAME="IDX59"></A>
<A NAME="IDX60"></A>


<P>
<A NAME="IDX61"></A>
<A NAME="IDX62"></A>
<A NAME="IDX63"></A>


<P>
Although there is a separate boolean type,
any Scheme value can be used as a boolean value for the purpose of a
conditional test.  As explained in section <A HREF="r5rs_8.html#SEC58">6.3.1 Booleans</A>, all
values count as true in such a test except for <TT>#f</TT>.
This report uses the word "true" to refer to any
Scheme value except <TT>#f</TT>, and the word "false" to refer to
<TT>#f</TT>.  
<A NAME="IDX64"></A>
<A NAME="IDX65"></A>




<H2><A NAME="SEC21" HREF="r5rs_toc.html#TOC21">3.3 External representations</A></H2>

<P>
<A NAME="IDX66"></A>


<P>
An important concept in Scheme (and Lisp) is that of the <EM>external
representation</EM> of an object as a sequence of characters.  For example,
an external representation of the integer 28 is the sequence of
characters "<TT>28</TT>", and an external representation of a list consisting
of the integers 8 and 13 is the sequence of characters "<TT>(8 13)</TT>".


<P>
The external representation of an object is not necessarily unique.  The
integer 28 also has representations "<TT>#e28.000</TT>" and "<TT>#x1c</TT>", and the
list in the previous paragraph also has the representations "<TT>( 08 13
)</TT>" and "<TT>(8 . (13 . ()))</TT>" (see section see section <A HREF="r5rs_8.html#SEC59">6.3.2 Pairs and lists</A>).


<P>
Many objects have standard external representations, but some, such as
procedures, do not have standard representations (although particular
implementations may define representations for them).


<P>
An external representation may be written in a program to obtain the
corresponding object (see <SAMP>`quote'</SAMP>, section see section <A HREF="r5rs_6.html#SEC27">4.1.2 Literal expressions</A>).


<P>
External representations can also be used for input and output.  The
procedure <SAMP>`read'</SAMP> (section see section <A HREF="r5rs_8.html#SEC68">6.6.2 Input</A>) parses external
representations, and the procedure <SAMP>`write'</SAMP> (section see section <A HREF="r5rs_8.html#SEC69">6.6.3 Output</A>)
generates them.  Together, they provide an elegant and powerful
input/output facility.


<P>
Note that the sequence of characters "<TT>(+ 2 6)</TT>" is <EM>not</EM> an
external representation of the integer 8, even though it <EM>is</EM> an
expression evaluating to the integer 8; rather, it is an external
representation of a three-element list, the elements of which are the symbol
<TT>+</TT> and the integers 2 and 6.  Scheme's syntax has the property that
any sequence of characters that is an expression is also the external
representation of some object.  This can lead to confusion, since it may
not be obvious out of context whether a given sequence of characters is
intended to denote data or program, but it is also a source of power,
since it facilitates writing programs such as interpreters and
compilers that treat programs as data (or vice versa).


<P>
The syntax of external representations of various kinds of objects
accompanies the description of the primitives for manipulating the
objects in the appropriate sections of chapter section <A HREF="r5rs_8.html#SEC48">6. Standard procedures</A>.




<H2><A NAME="SEC22" HREF="r5rs_toc.html#TOC22">3.4 Storage model</A></H2>

<P>
<A NAME="IDX67"></A>


<P>
Variables and objects such as pairs, vectors, and strings implicitly
denote locations or sequences of locations.  A string, for
<A NAME="IDX68"></A>
example, denotes as many locations as there are characters in the string. 
(These locations need not correspond to a full machine word.) A new value may be
stored into one of these locations using the <TT>string-set!</TT> procedure, but
the string continues to denote the same locations as before.


<P>
An object fetched from a location, by a variable reference or by
a procedure such as <SAMP>`car'</SAMP>, <SAMP>`vector-ref'</SAMP>, or <SAMP>`string-ref'</SAMP>, is
equivalent in the sense of <CODE>eqv?</CODE> 
(section see section <A HREF="r5rs_8.html#SEC49">6.1 Equivalence predicates</A>)
<A NAME="IDX69"></A>
to the object last stored in the location before the fetch.


<P>
Every location is marked to show whether it is in use.
No variable or object ever refers to a location that is not in use.
Whenever this report speaks of storage being allocated for a variable
or object, what is meant is that an appropriate number of locations are
chosen from the set of locations that are not in use, and the chosen
locations are marked to indicate that they are now in use before the variable
or object is made to denote them.


<P>
In many systems it is desirable for constants (i.e. the values of
<A NAME="IDX70"></A>
literal expressions) to reside in read-only-memory.  To express this, it is
convenient to imagine that every object that denotes locations is associated
with a flag telling whether that object is mutable or
<A NAME="IDX71"></A>
immutable.  In such systems literal constants and the strings
<A NAME="IDX72"></A>
returned by <CODE>symbol-&#62;string</CODE> are immutable objects, while all objects
<A NAME="IDX73"></A>
created by the other procedures listed in this report are mutable.  It is an
error to attempt to store a new value into a location that is denoted by an
immutable object.




<H2><A NAME="SEC23" HREF="r5rs_toc.html#TOC23">3.5 Proper tail recursion</A></H2>

<P>
<A NAME="IDX74"></A>


<P>
Implementations of Scheme are required to be
<EM>properly tail-recursive</EM>.
Procedure calls that occur in certain syntactic
contexts defined below are `tail calls'.  A Scheme implementation is
properly tail-recursive if it supports an unbounded number of active
tail calls.  A call is <EM>active</EM> if the called procedure may still
return.  Note that this includes calls that may be returned from either
by the current continuation or by continuations captured earlier by
<SAMP>`call-with-current-continuation'</SAMP> that are later invoked.
In the absence of captured continuations, calls could
return at most once and the active calls would be those that had not
yet returned.
A formal definition of proper tail recursion can be found
in <A HREF="r5rs_13.html#BIB8">[propertailrecursion]</A>.



<BLOCKQUOTE>
<P>
<EM>Rationale:</EM>


<P>
Intuitively, no space is needed for an active tail call because the
continuation that is used in the tail call has the same semantics as the
continuation passed to the procedure containing the call.  Although an improper
implementation might use a new continuation in the call, a return
to this new continuation would be followed immediately by a return
to the continuation passed to the procedure.  A properly tail-recursive
implementation returns to that continuation directly.


<P>
Proper tail recursion was one of the central ideas in Steele and
Sussman's original version of Scheme.  Their first Scheme interpreter
implemented both functions and actors.  Control flow was expressed using
actors, which differed from functions in that they passed their results
on to another actor instead of returning to a caller.  In the terminology
of this section, each actor finished with a tail call to another actor.


<P>
Steele and Sussman later observed that in their interpreter the code
for dealing with actors was identical to that for functions and thus
there was no need to include both in the language.


</BLOCKQUOTE>

<P>
A <EM>tail call</EM> is a procedure call that occurs
<A NAME="IDX75"></A>
in a <EM>tail context</EM>.  Tail contexts are defined inductively.  Note
that a tail context is always determined with respect to a particular lambda
expression.



<UL>

<LI>

The last expression within the body of a lambda expression,
shown as &#60;tail expression&#62; below, occurs in a tail context.


<PRE>
<TT>(lambda &#60;formals&#62;
  &#60;definition&#62;* &#60;expression&#62;* &#60;tail expression&#62;)
</TT>

</PRE>

<LI>

If one of the following expressions is in a tail context,
then the subexpressions shown as &#60;tail expression&#62; are in a tail context.
These were derived from rules in the grammar given in
chapter section <A HREF="r5rs_9.html#SEC71">7. Formal syntax and semantics</A> by replacing some occurrences of &#60;expression&#62;
with &#60;tail expression&#62;.  Only those rules that contain tail contexts
are shown here.


<PRE>
<TT>(if &#60;expression&#62; &#60;tail expression&#62; &#60;tail expression&#62;)
(if &#60;expression&#62; &#60;tail expression&#62;)

(cond &#60;cond clause&#62;+)
(cond &#60;cond clause&#62;* (else &#60;tail sequence&#62;))

(case &#60;expression&#62;
  &#60;case clause&#62;+)
(case &#60;expression&#62;
  &#60;case clause&#62;*
  (else &#60;tail sequence&#62;))

(and &#60;expression&#62;* &#60;tail expression&#62;)
(or &#60;expression&#62;* &#60;tail expression&#62;)

(let (&#60;binding spec&#62;*) &#60;tail body&#62;)
(let &#60;variable&#62; (&#60;binding spec&#62;*) &#60;tail body&#62;)
(let* (&#60;binding spec&#62;*) &#60;tail body&#62;)
(letrec (&#60;binding spec&#62;*) &#60;tail body&#62;)

(let-syntax (&#60;syntax spec&#62;*) &#60;tail body&#62;)
(letrec-syntax (&#60;syntax spec&#62;*) &#60;tail body&#62;)

(begin &#60;tail sequence&#62;)

(do (&#60;iteration spec&#62;*)
    (&#60;test&#62; &#60;tail sequence&#62;)
  &#60;expression&#62;*)

where

&#60;cond clause&#62; --&#62; (&#60;test&#62; &#60;tail sequence&#62;)
&#60;case clause&#62; --&#62; ((&#60;datum&#62;*) &#60;tail sequence&#62;)

&#60;tail body&#62; --&#62; &#60;definition&#62;* &#60;tail sequence&#62;
&#60;tail sequence&#62; --&#62; &#60;expression&#62;* &#60;tail expression&#62;
</TT>

</PRE>

<LI>

If a <SAMP>`cond'</SAMP> expression is in a tail context, and has a clause of
the form <SAMP>`(&#60;expression1&#62; =&#62; &#60;expression2&#62;)'</SAMP>
then the (implied) call to
the procedure that results from the evaluation of &#60;expression2&#62; is in a
tail context.  &#60;expression2&#62; itself is not in a tail context.

</UL>

<P>
Certain built-in procedures are also required to perform tail calls.
The first argument passed to <CODE>apply</CODE> and to
<A NAME="IDX76"></A>
<CODE>call-with-current-continuation</CODE>, and the second argument passed to
<A NAME="IDX77"></A>
<CODE>call-with-values</CODE>, must be called via a tail call.
<A NAME="IDX78"></A>
Similarly, <CODE>eval</CODE> must evaluate its argument as if it
<A NAME="IDX79"></A>
were in tail position within the <CODE>eval</CODE> procedure.


<P>
In the following example the only tail call is the call to <SAMP>`f'</SAMP>.
None of the calls to <SAMP>`g'</SAMP> or <SAMP>`h'</SAMP> are tail calls.  The reference to
<SAMP>`x'</SAMP> is in a tail context, but it is not a call and thus is not a
tail call.



<PRE>

(lambda ()
  (if (g)
      (let ((x (h)))
        x)
      (and (g) (f))))

</PRE>


<BLOCKQUOTE>
<P>
<EM>Note:</EM>
Implementations are allowed, but not required, to
recognize that some non-tail calls, such as the call to <SAMP>`h'</SAMP>
above, can be evaluated as though they were tail calls.
In the example above, the <SAMP>`let'</SAMP> expression could be compiled
as a tail call to <SAMP>`h'</SAMP>. (The possibility of <SAMP>`h'</SAMP> returning
an unexpected number of values can be ignored, because in that
case the effect of the <SAMP>`let'</SAMP> is explicitly unspecified and
implementation-dependent.)
</BLOCKQUOTE>

<P>
       


<P><HR><P>
Go to the <A HREF="r5rs_1.html">first</A>, <A HREF="r5rs_4.html">previous</A>, <A HREF="r5rs_6.html">next</A>, <A HREF="r5rs_14.html">last</A> section, <A HREF="r5rs_toc.html">table of contents</A>.
</BODY>
</HTML>
